<template>
  <div>
    <iframe class="J_iframe" name="editFromTempiframe" id="editFromTempiframe" style="width: calc(100vw - 20px );height: calc(100vh - 15px);" frameborder="0" src="/flow/Flowdesign.html" @load="FlowDesignLoad"></iframe>
    <formdesign ref="formdesign"></formdesign>
    <zhlDialog title="表单选择" ref="selectfromdialog" width="1260px" class="my-dialog">
      <selectFrom :fromtype="5" @selectconfirm="selectFromConfirm"></selectFrom>
    </zhlDialog>
    <div style="display:none">
      <selector ref="selectoruser" :typemode="1" :ismulti="true" :checklisted="checklisted" @confirm="selectUserConfirm" :isselectModel="true"></selector>
    </div>
  </div>
</template>
<script>
import formdesign from '@/views/fromdesign/index.vue'
import selectFrom from '@/views/Flow/selectFrom.vue'
import { queryByProcessId } from '@/api/tappProcesschart.js'
import { optSaveFlowData, optDesignStartFlow, optStopFlow, optSaveAsFlow, optCheackProcesscode } from '@/api/tappProcess.js'
export default {
  props: ['processId', 'processState'],
  created () {
    window.addEventListener('message', this.EditFrom, false)
  },
  components: { formdesign, selectFrom },
  methods: {
    EditFrom (func) {
      this.currentfunname = func.data.funname
      if (func.data.funname === 'EditFrom') {
        if (
          func.data.fromid === undefined ||
          func.data.fromid === null ||
          func.data.fromid === ''
        ) {
          this.$refs.formdesign.addfrom('5')
        } else {
          this.$refs.formdesign.editfrom(func.data.fromid)
        }
      } else if (func.data.funname === 'selectFrom') {
        this.selectFrom()
      } else if (func.data.funname === 'selectStartPerson') {
        this.selectStartPerson()
      } else if (func.data.funname === 'selectReadPerson') {
        this.selectReadPerson()
      } else if (func.data.funname === 'selectApprover') {
        this.selectApprover()
      } else if (func.data.funname === 'SaveFlowData') {
        this.SaveFlowData(func.data.param, func.data.callfun)
      } else if (func.data.funname === 'DesignStartFlow') {
        this.DesignStartFlow(func.data.param, func.data.callfun)
      } else if (func.data.funname === 'StopFlow') {
        this.StopFlow(func.data.param, func.data.callfun)
      } else if (func.data.funname === 'SaveAsFlow') {
        this.SaveAsFlow(func.data.param, func.data.callfun)
      } else if (func.data.funname === 'CheackProcesscode') {
        this.CheackProcesscode(func.data.param, func.data.callfun)
      } else if (func.data.funname === 'success') {
        this.$message.success(func.data.msg)
      } else if (func.data.funname === 'error') {
        this.$message.error(func.data.msg)
      } else if (func.data.funname === 'warning') {
        this.$message.warning(func.data.msg)
      }
    },
    selectFrom () {
      this.$refs.selectfromdialog.visible = true
    },
    selectStartPerson () {
      this.checklisted = []
      this.$refs.selectoruser.selectsearch()
    },
    selectReadPerson () {
      this.checklisted = []
      this.$refs.selectoruser.selectsearch()
    },
    selectApprover () {
      this.checklisted = []
      this.$refs.selectoruser.selectsearch()
    },
    selectFromConfirm (rows) {
      const editFromTempiframe = this.getFlowDesignIframe()
      if (editFromTempiframe !== null) {
        editFromTempiframe.flowdataObj.selectFromConfirm(rows)
        this.$refs.selectfromdialog.visible = false
      }
    },
    getFlowDesignIframe () {
      if (
        window.frames.editFromTempiframe !== undefined &&
        window.frames.editFromTempiframe.flowdataObj !== undefined &&
        typeof window.frames.editFromTempiframe.flowdataObj.UpdateVariable !==
        'undefined'
      ) {
        return window.frames.editFromTempiframe
      }
      return null
    },
    selectUserConfirm (userlist, typemode) {
      const editFromTempiframe = this.getFlowDesignIframe()
      if (editFromTempiframe !== null) {
        editFromTempiframe.flowdataObj.selectUserConfirm(
          userlist,
          this.currentfunname, typemode
        )
      }
    },
    FlowDesignLoad () {
      this.loadFlowDesign(this.processId, this.processState)
    },
    loadFlowDesign (processId, processState) {
      const editFromTempiframe = window.frames.editFromTempiframe
      if (editFromTempiframe !== null) {
        if (processId !== undefined && processId !== '') {
          queryByProcessId(processId).then(res => {
            if (res.code === 0) {
              res.data.ProcessState = processState
              editFromTempiframe.LoadDataFlowInit(res.data, processId)
            } else {
              editFromTempiframe.LoadDataFlowInit()
            }
          })
        } else {
          editFromTempiframe.LoadDataFlowInit()
        }
      }
    },
    SaveFlowData (param) {
      const editFromTempiframe = window.frames.editFromTempiframe
      optSaveFlowData(param).then(res => {
        editFromTempiframe.flowdataObj.callfun(res)
      })
    },
    DesignStartFlow (param) {
      const editFromTempiframe = window.frames.editFromTempiframe
      optDesignStartFlow(param).then(res => {
        editFromTempiframe.flowdataObj.callfun(res)
      })
    },
    StopFlow (FlowID) {
      const editFromTempiframe = window.frames.editFromTempiframe
      optStopFlow(FlowID).then(res => {
        editFromTempiframe.flowdataObj.callfun(res)
      })
    },
    SaveAsFlow (param) {
      const editFromTempiframe = window.frames.editFromTempiframe
      optSaveAsFlow(param).then(res => {
        editFromTempiframe.flowdataObj.callfun(res)
      })
    },
    CheackProcesscode (param) {
      const editFromTempiframe = window.frames.editFromTempiframe
      optCheackProcesscode(param).then(res => {
        editFromTempiframe.flowdataObj.callfun(res)
      })
    }
  },
  data () {
    return {
      currentfunname: '',
      checklisted: []
    }
  }
}
</script>
